package gui;

import javax.swing.*;

import model.Customer;
import database.CustomerOperations;
import database.EstateOperations;
import database.OrderOperations;
import database.RoadOperations;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

@SuppressWarnings("serial")
public class tab4 extends JPanel implements ItemListener,ActionListener
{
	JPanel p1 ,p2, p3;
	JButton find, update, clear;
	JLabel l2,l3,l4,l5,l6,l7,l8;
	JTextField t2,t3,t4,t5,t6;
	private JComboBox <String> est, road1, house,date1, date2;	
	private String estName;
	private String roadName;
	private String houseName;
	private EstateOperations eo;
	private RoadOperations ro;
	private CustomerOperations co;
	private ArrayList<String> ests;
	private ArrayList<String> hse;
	private ArrayList<String> rds;
	private OrderOperations oo;
	private String dates1[] = {"00/00/00"};
	private String dates2[] = {"00/00/00"};
	
	public tab4() 
	{
		this.setPreferredSize(new Dimension(795, 395));
		eo=new EstateOperations();
		ro=new RoadOperations();
		co=new CustomerOperations();
		oo=new OrderOperations();
		
		//Panel 1 buttons and combos
		p1 = new JPanel();
		p1.setLayout(new FlowLayout());
		p1.setPreferredSize(new Dimension(795, 75));
		
		//list of estates in combo box
		est = new JComboBox<String>();	
		est.setPreferredSize(new Dimension(100,25));
		ests=new ArrayList<String>();
		ests=eo.getEstateNames();
		for(int i=0;i<ests.size();i++)
		{
			est.addItem(ests.get(i));
		}
		est.addItemListener(this);
		estName=(String)est.getSelectedItem();
		
		//list of roads in combo box
		road1 = new JComboBox<String>();	
		road1.setPreferredSize(new Dimension(100,25));
		rds=new ArrayList<String>();
		rds=ro.getRoadNames(estName);
		for(int i=0;i<rds.size();i++)
		{
			road1.addItem(rds.get(i));
		}
		road1.addItemListener(this);
		roadName=(String)road1.getSelectedItem();
		
		//list of houses in combo box
		house = new JComboBox<String>();	
		house.setPreferredSize(new Dimension(100,25));
		hse=new ArrayList<String>();
		hse=co.getHouseNames(roadName,co.getEstId(estName));
		for(int i=0;i<hse.size();i++)
		{
			house.addItem(hse.get(i));
		}
		house.addItemListener(this);
		houseName=(String)house.getSelectedItem();
		
		find = new JButton("FIND");
		find.addActionListener(this);
		
		p1.add(est);
		p1.add(road1);
		p1.add(house);
		p1.add(Box.createRigidArea(new Dimension(70,0)));
		p1.add(find);
		
		
		//P2
		p2 = new JPanel();
		p2.setLayout(new GridLayout(5,4));
		p2.setPreferredSize(new Dimension(750, 120));
		
		l2 = new JLabel("Customer number: ");
		l3 = new JLabel("E-mail address: ");
		l4 = new JLabel("First name: ");
		l5 = new JLabel("Phone number: ");
		l6 = new JLabel("Last name: ");
		l7 = new JLabel("Suspend order from: ");
		l8 = new JLabel("To: ");
		
		t2 = new JTextField(15);
		t2.setEditable(false);
		t3 = new JTextField(15);
		t4 = new JTextField(15);
		t4.setEditable(false);
		t5 = new JTextField(15);
		t6 = new JTextField(15);
		t6.setEditable(false);
		date1 = new JComboBox<>(dates1);
		date2 = new JComboBox<>(dates2);
		
		//Last grid in this panel contains multiple components, need to put them in a panel
		JPanel container = new JPanel();
		//container.setLayout(new FlowLayout());
		
		container.add(date1);
		container.add(l8);
		container.add(date2);
		JLabel s1 = new JLabel(" ");JLabel s2 = new JLabel(" ");JLabel s3 = new JLabel(" ");JLabel s4 = new JLabel(" ");
		JLabel s5 = new JLabel(" ");JLabel s6 = new JLabel(" ");JLabel s7 = new JLabel(" ");JLabel s8 = new JLabel(" ");
		
		
		p2.add(l2);
		p2.add(t2);
		p2.add(l3);
		p2.add(t3);
		p2.add(s1);p2.add(s2);p2.add(s3);p2.add(s4);
		p2.add(l4);
		p2.add(t4);
		p2.add(l5);
		p2.add(t5);
		p2.add(s5);p2.add(s6);p2.add(s7);p2.add(s8);
		p2.add(l6);
		p2.add(t6);
		p2.add(l7);
		p2.add(container);
		
		
		
		
		//Panel 3
		
		p3 = new JPanel();
		
		update = new JButton("UPDATE");
		update.addActionListener(this);
		clear =  new JButton("CLEAR");
		clear.addActionListener(this);
		p3.add(Box.createRigidArea(new Dimension(0,70)));
		p3.add(update);
		p3.add(Box.createRigidArea(new Dimension(50,0)));
		p3.add(clear);
		
		
		
		this.add(p1, BorderLayout.NORTH);
		this.add(p2, BorderLayout.CENTER);
		this.add(p3, BorderLayout.SOUTH);
		
		
		
	}

	
			
	@Override
	public void itemStateChanged(ItemEvent ae) 
	{
		if (ae.getSource() == est && ae.getStateChange() == ItemEvent.SELECTED)
		{
			estName=(String)est.getSelectedItem();
			rds=ro.getRoadNames(estName);
			road1.removeAllItems();
			for(int i=0;i<rds.size();i++)
			{
				road1.addItem(rds.get(i));
			}
		}
		if (ae.getSource() == road1 && ae.getStateChange() == ItemEvent.SELECTED)
		{
			roadName=(String)road1.getSelectedItem();
			hse=co.getHouseNames(roadName,co.getEstId(estName));
			house.removeAllItems();
			for(int i=0;i<hse.size();i++)
			{
				house.addItem(hse.get(i));
			}
		}
		if (ae.getSource() == house && ae.getStateChange() == ItemEvent.SELECTED)
		{
			houseName=(String)house.getSelectedItem();
		}
	}

	//find a customer by address
	public void find()
	{
		int num=co.getEstId(estName);
		Customer c=co.find(houseName,co.getRoadId(num,roadName));
		int a=oo.findOrdNum(c.getId());
		if(a==0)
		{
			
		}
		else
		{
			t2.setText(String.valueOf(c.getId()));
			t3.setText(c.getEmail());
			t4.setText(c.getFname());
			t5.setText(String.valueOf(c.getPhone()));
			t6.setText(c.getLname());
		}
		
	}

	
	@Override
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==update)
		{
			try 
			{
				co.updateCustomerPhone(Integer.parseInt(t2.getText()),Integer.parseInt(t5.getText()));
				co.updateCustomerEmail(Integer.parseInt(t2.getText()),t3.getText());
				JOptionPane.showMessageDialog(null,
						"Customer details updated","Customer Update",JOptionPane.WARNING_MESSAGE);
			}
			catch (NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null,
						"You must enter only numbers for the phone number","ERROR",JOptionPane.WARNING_MESSAGE);
			} 
			
		}
		if(ae.getSource()==find)
		{
			find();
		}
		if(ae.getSource()==clear)
		{
			t2.setText("");
			t3.setText("");
			t4.setText("");
			t5.setText("");
			t6.setText("");
		}
		
	}
	
	
}
